home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / c / faslhp800.c < prev    next >
C/C++ Source or Header  |  1991-04-26  |  4KB  |  164 lines

  1. /* round up i to be a multiple of (n) */
  2. #define ROUND_UP(i,n) ((((int) (i) + n-1)/(n)) *(n))
  3.  
  4. #define    MAXPATHLEN    512
  5. int init_address = 0;
  6.  
  7. #ifdef HPUX_SOM
  8. #include <a.out.h>
  9.  
  10.  
  11. #define GET_HEADERS(fp) \
  12.  struct header hdr; \
  13.  struct som_exec_auxhdr somhdr; \
  14.  if (sizeof(hdr) !=fread(&hdr,1,sizeof(hdr),fp)) \
  15.     {FEerror("could not read header");} \
  16.   if (hdr.aux_header_size) \
  17.     {fseek(fp,hdr.aux_header_location,0); \
  18.        if (sizeof(somhdr) != fread(&somhdr,1,sizeof(somhdr),fp)) \
  19.         {FEerror("could not read header");}} \
  20.    else { somhdr.exec_tsize=0;somhdr.exec_dsize=0;somhdr.exec_bsize=0;}
  21.  
  22.  
  23.  
  24. #define SET_TOTAL_SPACE(total,fp) \
  25.   total= ROUND_UP(somhdr.exec_tsize,sizeof(double)) \
  26.     + somhdr.exec_dsize + somhdr.exec_bsize;\
  27.  
  28. #define READ_FASL_TO_MEMORY(memory,fp) \
  29. do{ char *p = memory->cfd.cfd_start; \
  30.   init_address = ((char *)somhdr.exec_entry - p); \
  31.   if (init_address > 1000) printf("entry is offset at %x(%d)",init_address); \
  32.     fseek(fp,somhdr.exec_tfile,0) ; \
  33.   fread(p,1,somhdr.exec_tsize,fp); \
  34.   fseek(fp,somhdr.exec_dfile,0) ; \
  35.     if ((int)(p + *data_off) != somhdr.exec_dmem) \
  36.       FEerror("bad data offset");\
  37.   fread(p+ *data_off,1, \
  38.     somhdr.exec_dsize,fp); \
  39. }while(0)
  40.  
  41. #define TXT_ALIGN 4096
  42.  
  43. #undef BSD
  44. #endif
  45.  
  46.  
  47. #ifdef BSD
  48. #include <a.out.h>
  49. #define GET_HEADERS(fp) \
  50.    struct exec hdr; fseek(fp,0,0);\
  51.     fread(&hdr,1,sizeof(hdr),fp); \
  52. #define SET_TOTAL_SPACE(total,fp) \
  53.     total = hdr.a_txtsize +     hdr.a_datasize + header.a_bss;
  54. #define READ_FASL_TO_MEMORY(memory,fp) \
  55.  fseek(fp,sizeof(struct header) ,0); \
  56.    fread(memory->cfd.cfd_start,1,memory->cfd.cfd_size,fp); \
  57.  
  58. #endif
  59.  
  60.  
  61.  
  62.  
  63. read_text_and_data(memory,fp,data_off)
  64.      object memory;
  65.      FILE *fp;
  66.      int *data_off;
  67. { int total;
  68.   GET_HEADERS(fp);
  69.   SET_TOTAL_SPACE(total,fp);
  70.   *data_off = ROUND_UP(somhdr.exec_tsize,sizeof(double));
  71.   if (total > memory->cfd.cfd_size)
  72.     { memory->cfd.cfd_size = total; return 0;}
  73.   
  74.   READ_FASL_TO_MEMORY(memory,fp);
  75.   return 1;
  76. }
  77.  
  78. #ifndef  TXT_ALIGN
  79. #define TXT_ALIGN sizeof(double)
  80. #endif
  81.  
  82. static
  83. fasload_help(faslfile,lib_string)
  84. object faslfile;
  85. char *lib_string;
  86. { char filename[MAXPATHLEN],tmpfile[MAXPATHLEN];
  87.    char command [2*MAXPATHLEN]; 
  88.    int total;
  89.    object memory ;
  90.    FILE *fp;
  91.  
  92.    faslfile = open_stream(faslfile, smm_input, Cnil, Kerror);
  93.    fp = faslfile->sm.sm_fp;
  94.  
  95.  
  96.   { int data_off = 0;
  97.     GET_HEADERS(fp);
  98.     SET_TOTAL_SPACE(total,fp);
  99.     memory=alloc_object(t_cfdata);
  100.     memory->cfd.cfd_self = 0;
  101.     memory->cfd.cfd_start = 0;
  102.     memory->cfd.cfd_size = total;
  103.    sprintf(tmpfile,"/tmp/fasl%d",getpid());
  104.  
  105.  AGAIN:   
  106.  
  107.    memory->cfd.cfd_start = ALLOC_ALIGNED(alloc_contblock,
  108.                      memory->cfd.cfd_size,TXT_ALIGN);
  109.    
  110.  
  111.    coerce_to_filename(faslfile, filename);
  112.    LD_COMMAND(command,
  113.         kcl_self,
  114.         memory->cfd.cfd_start,
  115.         filename,
  116.         (lib_string ? lib_string : " "),
  117.         tmpfile);
  118.     if (system(command) != 0)
  119.       { FEerror("The linkage editor failed.", 0);}
  120.     if(symbol_value(Vload_verbose)==Vload_verbose)
  121.       { printf("%s\n",command); fflush(stdout);}
  122.  
  123.    {FILE *fp1 = fopen(tmpfile,"r");
  124.     if (fp1==0) {FEerror("Couldn't open tmpfile");}
  125.     if(read_text_and_data(memory,fp1,&data_off) ==0)
  126.       {fclose(fp1); goto AGAIN;}
  127.     fclose(fp1);
  128.   }}
  129.   unlink(tmpfile);
  130.   SEEK_TO_END_OFILE(fp);
  131.   call_init(init_address,memory,read_fasl_vector(faslfile));
  132.   close_stream (faslfile,TRUE);
  133.   if(symbol_value(Vload_verbose)!=Cnil)
  134.     printf("start address -T %x ",memory->cfd.cfd_start);
  135.   return(memory->cfd.cfd_size);
  136. }
  137.  
  138. fasload(faslfile)
  139.      object faslfile;
  140. {return fasload_help(faslfile,0);}
  141.  
  142. #define FASLINK
  143.  
  144. siLfaslink()
  145. { object *base = vs_base;
  146.   check_arg(2);
  147.   vs_base[0] = make_fixnum(fasload_help(base[0],object_to_string(base[1])));
  148.   printf("done link"); fflush(stdout);
  149.   vs_top = vs_base+1;
  150.  return ;
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. int
  160. faslink(faslfile, ldargstring)
  161. object faslfile, ldargstring;
  162. {printf("later");}
  163.  
  164.